package cn.uniquetech.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @param
 * @author: zjn
 * @Date: 2018/9/11 0011 16:18
 * @Description:
 */
public class RestartIncreament {

    public static final String PACKAGENAME = "cn.uniquetech";
    private static final String URL = "jdbc:db2://139.196.177.17:50000/xzh";
    private static final String NAME = "db2admin";
    private static final String PASS = "XZuniquesoft2016";
    private static final String DRIVER = "com.ibm.db2.jcc.DB2Driver";



    public static void main(String[] args){

        //获取表明
        String db_sql = "select tabname from syscat.tables where tabschema='DB2ADMIN' and type = 'T' ";
        List<String> dbList = new ArrayList<>();
        try {
            Class.forName(DRIVER);
            Connection con = DriverManager.getConnection(URL, NAME, PASS);
            PreparedStatement ps = null;
            ResultSet rs = null;
            ps = con.prepareStatement(db_sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                String tabname = rs.getString("tabname").toLowerCase();
                dbList.add(tabname);
            }
            for(String tableName : dbList){
                try {
                    System.out.println("-------------------------->>> tableName:" + tableName);
                    String query_sql = "select max(iid) maxid from " + tableName;
                    ps = con.prepareStatement(query_sql);
                    rs = ps.executeQuery();
                    rs.next();
                    int maxId = rs.getInt("maxid");
                    System.out.println("------------------------->>> "+tableName + ",maxid:" + maxId);
                    String update_restart_sql = "ALTER TABLE db2admin."+tableName+" ALTER COLUMN iid RESTART WITH "+(maxId+1);
                    ps = con.prepareStatement(update_restart_sql);
                    ps.executeUpdate();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            rs.close();
            ps.close();
            con.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
